<template>
    <div class="main-container">
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span><i class="el-icon-condition"></i></span>
            </div>
            <div style="height: 40px;">
                <el-form :inline="true" :model="formQuery">
                    <el-form-item label="权限名称">
                        <el-input size="small" v-model="formQuery.name" placeholder="权限名称"></el-input>
                    </el-form-item>
                    <el-form-item label="权限类型">
                        <el-select size="small" v-model="formQuery.type" placeholder="权限类型">
                            <el-option label="全部" value=""></el-option>
                            <el-option v-for="(item,index) in permissionType" :key="index" :label="item.value"
                                       :value="item.code"/>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="权限状态">
                        <el-select size="small" v-model="formQuery.status" placeholder="权限状态">
                            <el-option label="全部" value=""></el-option>
                            <el-option label="启用" value="1"></el-option>
                            <el-option label="禁用" value="0"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" @click="onSubmit(null,null)" size="small">查询
                        </el-button>
                        <el-button type="info" icon="el-icon-refresh-left" @click="onReset" size="small">
                            重置
                        </el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
        <el-card class="table-list">
            <div slot="header" class="clearfix">
                <span><i class="el-icon-table-list" style="font-weight: 600;margin-top: 8px;"></i></span>
                <el-button class="el-icon-s-operation" style="float: right; padding: 3px 0;font-size: 20px" type="text"
                           @click="handleClick(null)"></el-button>
            </div>
            <div>
                <el-table :data="tableData" height="calc(100vh - 400px)" border style="width: 100%"
                          empty-text="暂无数据" :loading="loading">
                    <el-table-column type="index" width="50" label="序号"></el-table-column>
                    <el-table-column align="center" width="120" prop="type" label="权限类型"></el-table-column>
                    <el-table-column align="center" width="120" prop="code" label="权限编码"></el-table-column>
                    <el-table-column align="center" prop="name" label="权限名称"></el-table-column>
                    <el-table-column align="center" prop="description" label="权限描述"></el-table-column>
                    <el-table-column align="center" prop="status" label="权限状态" width="100">
                        <template slot-scope="scope">
                            <span v-if="scope.row.status ===1" style="color: #92e268">启用</span>
                            <span v-else style="color: #ff2b4a">禁用</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" fixed="right" label="操作" width="100">
                        <template slot-scope="scope">
                            <el-button @click="handleClick(scope.row)" type="text"
                                       icon="el-icon-edit-outline"
                                       style="font-size: 18px">
                            </el-button>
                            <el-popconfirm title="确定删除吗?" style="margin-left: 10px;"
                                           @onConfirm="handleDelete(scope.row)">
                                <el-button type="text" icon="el-icon-delete"
                                           style="font-size: 18px"
                                           slot="reference">
                                </el-button>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div style="text-align: right;margin-top: 8px">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                               :current-page="currentPage"
                               :page-sizes="[10, 20, 50, 100]"
                               :page-size="pageSize"
                               layout="prev, pager, next, sizes,total"
                               :total="total">
                </el-pagination>
            </div>
        </el-card>
        <permission-edit :dialogVisible="dialogVisible" :editTitle="editTitle" :rowData="rowData"
                         @callBackFromSub="callBackFromSub"></permission-edit>
    </div>
</template>

<script>
    import PermissionEdit from "./PermissionEdit"

    export default {
        name: "PermissionManager",
        data() {
            return {
                tableData: [],
                rowData: {},
                currentPage: 1,
                pageSize: 10,
                total: 0,
                loading: false,
                formQuery: {
                    name: '',
                    type: '',
                    status: ''
                },
                permissionType: [],
                editTitle: "提示",
                dialogVisible: false
            }
        },
        components: {
            PermissionEdit
        },
        methods: {
            handleClick(row) {
                if (row) {
                    this.dialogVisible = true;
                    this.editTitle = "修改权限";
                    this.rowData = row;
                } else {
                    this.dialogVisible = true;
                    this.editTitle = "添加权限";
                    this.rowData = {};
                }
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.onSubmit(null, val);
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.onSubmit(val, this.pageSize);
            },
            //查询方法
            onSubmit(currentPage, pageSize) {
                this.formQuery.currentPage = currentPage ? currentPage : 1;
                this.formQuery.pageSize = pageSize ? pageSize : this.pageSize;
                this.initTableData(this.formQuery);
            },
            onReset() {
                this.formQuery = {
                    name: '',
                    type: '',
                    status: ''
                };
            },
            querySelect(type) {
                this.$httpGet(this.$axiosCommonHttp, "api/rest/dict/kv/" + type, {}).then(res => {
                    if (res.code === 200 && res.data) {
                        this.permissionType = res.data.filter(o => o['parentId'] !== "0");
                    }
                })
            },
            handleDelete(row) {
                this.$httpDelete(this.$axiosBaseHttp, "api/rest/auth/del/" + row.id, {}).then(res => {
                    if (res.code === 200) {
                        this.$successMsg("删除成功");
                    } else {
                        this.$errorMsg("删除失败");
                    }
                    this.onSubmit(null, null);
                });
            },
            //子组件方法回调
            callBackFromSub(param) {
                this.dialogVisible = param;
                this.initTableData(this.formQuery);
            },
            //初始化表格数据
            initTableData(param) {
                this.loading = true;
                this.$httpGet(this.$axiosBaseHttp, "api/rest/auth/list/page", param).then(res => {
                    if (res.code === 200) {
                        this.total = res.data.total;
                        this.pageSize = res.data.size;
                        this.tableData = res.data.records;
                    }
                    this.loading = false;
                })
            }
        },
        mounted() {
            this.querySelect("power");
            this.onSubmit(null, null);
        }
    }
</script>

<style scoped lang="scss">
    .clearfix:before,
    .clearfix:after {
        display: table;
        content: "";
    }

    .clearfix:after {
        clear: both
    }

    .clearfix > span {
        display: inline-block;
        height: 30px;
        line-height: 15px;
    }

    .el-icon-condition, .el-icon-table-list {
        font-size: 20px;
        margin: 3px 3px 3px 3px;
    }

    .table-list {
        margin-top: 10px;
        /*height: calc(100vh);*/
    }

    .el-card {
        /deep/ .el-card__header {
            padding: 10px 15px;
        }
    }

    .avatar {
        width: 35px;
        height: 35px;
        border-radius: 35px;
    }

    .el-table .cell {
        height: 23px;

        .el-button {
            padding: 0 0;
        }
    }
</style>